﻿using System.Collections.Generic;
using System.Text;
using System.Web.Mvc;

namespace AppManager.Background
{
    /// <summary>
    /// 将服务器端定义的变量输出到js文件中
    /// </summary>
    public class JsParameterController : Controller
    {
        // GET: JsParameter
        public void Index()
        {
            //最總要輸出的是一個文本文件 var a='b';var c='d';
            var dict = new Dictionary<string, object>();

            dict.Add("wysiwyg_color_Src", Url.Content("~/assets/plugins/bootstrap-wysihtml5/wysiwyg-color.css"));
            var str = GetJsStr(dict);

            Response.ContentType = "application/javascript";
            Response.Write(str);
            Response.End();
        }

        public string GetJsStr(Dictionary<string, object> dict)
        {
            var sb = new StringBuilder();
            foreach (var item in dict.Keys)
            {
                if (!string.IsNullOrEmpty(item) &&
                    !string.IsNullOrEmpty(dict[item]?.ToString()))
                    sb.AppendFormat("var {0}='{1}';", item, dict[item]);
            }
            return sb.ToString();
        }
    }
}